package com.navinfo.tripanalysis.offline.service.impl.huashen;

import com.navinfo.tripanalysis.common.pojo.PointProtocol;
import com.navinfo.tripanalysis.common.pojo.RunningType;
import com.navinfo.tripanalysis.common.pojo.RunningTypeEnum;
import com.navinfo.tripanalysis.offline.accumulator.DCConstants;
import com.navinfo.tripanalysis.offline.service.DataCleaningUpdAbnormalPointService;
import com.navinfo.tripanalysis.offline.util.AccumulatorUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 一汽数据清洗：修正点异常
 */
@RunningType(value = RunningTypeEnum.HS, interfaceClass = DataCleaningUpdAbnormalPointService.class )
public class DataCleaningUpdAbnormalPointServiceImpl implements DataCleaningUpdAbnormalPointService {

    @Override
    public PointProtocol DataUpdBy0F37(PointProtocol pre, PointProtocol cur, double speedMax, int rotationMax) {
        if(!"".equalsIgnoreCase(cur.getDataUpd())&&cur.getDataUpd() != null){
            List<String> updList = new ArrayList<>();
            if(cur.getDataUpd().contains("|")){
                updList = Arrays.asList(cur.getDataUpd().split("\\|"));
            }else{
                updList.add(cur.getDataUpd());
            }

            for(String col :updList){
                String key = col.split("=")[0];
                String value = col.split("=")[1];
                switch (key){
                    //case DCConstants.UPDFIELD_SPEED:if(cur.getSpeedKmh() > speedMax){cur.setSpeedKmh(pre.getSpeedKmh());}
                    case DCConstants.UPDFIELD_SPEED:
                        if(Double.parseDouble(value) > speedMax){
                            cur.setDataUpd(AccumulatorUtils.setFieldInConcatString(cur.getDataUpd(),"\\|",DCConstants.UPDFIELD_SPEED,String.valueOf(pre.getSpeedKmh())));
                        }
                        break;
                    //case DCConstants.UPDFIELD_ROTATION:if(cur.getRotation() > rotationMax){cur.setRotation(pre.getRotation());}
                    case DCConstants.UPDFIELD_ROTATION:
                        if(Integer.parseInt(value) > rotationMax){
                            cur.setDataUpd(AccumulatorUtils.setFieldInConcatString(cur.getDataUpd(),"\\|",DCConstants.UPDFIELD_ROTATION,String.valueOf(pre.getRotation())));
                        }
                        break;
                    default:break;
                }
            }
        }
        return cur;
    }
}
